package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentStockCkHistory;
import com.mxpio.erp.common.equipment.service.EquipmentStockCkHistoryService;
import com.mxpio.erp.common.equipment.vo.VEquipmentStockCkHistoryDetail;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: tpm_stock_ck_history
 * @Author: MxpIO
 * @Date:   2021-10-14
 * @Version: V1.0
 */
@Service
public class EquipmentStockCkHistoryServiceImpl extends BaseServiceImpl<EquipmentStockCkHistory> implements EquipmentStockCkHistoryService {

    @Override
    public Page<EquipmentStockCkHistory> queryCkApplyHistoryByMainId(Pageable page, String mainId) {
        String sql = "select\n" +
                "\t\t\t*,\n" +
                "\t\t\t(case when `type` = 0 then  '接收' else '拒绝' end) as optType\n" +
                "\t\tfrom MB_ERP_EQUIPMENT_STOCK_CK_HISTORY\n" +
                "\t\twhere ck_apply_id = "+mainId;
        List resultList = JpaUtil.nativeQuery(sql, EquipmentStockCkHistory.class).getResultList();
        String countSql = sql + " limit "  + (page.getPageNumber()-1)*page.getPageSize() + ","+page.getOffset();
        List total = JpaUtil.nativeQuery(countSql).getResultList();
        return new PageImpl<EquipmentStockCkHistory>(resultList, page, total.size());
    }

    @Override
    public Page<VEquipmentStockCkHistoryDetail> queryCkApplyHistoryDetailByMainId(Pageable page, EquipmentStockCkHistory tpmStockCkHistory) {
        String sql = "SELECT\n" +
                "            ch.id,\n" +
                "            ch.current_out_number,\n" +
                "            ca.stock_id,\n" +
                "            cai.materials_id,\n" +
                "            ti.batch AS batchNo,\n" +
                "            tm.NAME AS materialsName,\n" +
                "            tm.CODE AS materialsCode,\n" +
                "            tm.product_name AS materialsType\n" +
                "        FROM\n" +
                "            tpm_stock_ck_history ch\n" +
                "            LEFT JOIN tpm_stock_ck_apply ca ON ca.id = ch.ck_apply_id\n" +
                "            LEFT JOIN tpm_stock_ck_apply_item cai ON cai.id = ch.ck_apply_item_id\n" +
                "            LEFT JOIN tpm_stock_item_inventory ti ON ti.id = ch.inventory_id\n" +
                "            LEFT JOIN tpm_material tm ON tm.id = cai.materials_id\n" +
                "        WHERE\n" +
                "            ch.id = " + tpmStockCkHistory.getId();
        List resultList = JpaUtil.nativeQuery(sql, VEquipmentStockCkHistoryDetail.class).getResultList();
        String countSql = sql + " limit "  + (page.getPageNumber()-1)*page.getPageSize() + ","+page.getOffset();
        List total = JpaUtil.nativeQuery(countSql).getResultList();
        return new PageImpl<VEquipmentStockCkHistoryDetail>(resultList, page, total.size());
    }
}
